package org.xinjiang.tarim.travel.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xinjiang.tarim.travel.model.Poi;
import org.xinjiang.tarim.travel.service.PoiService;
import org.xinjiang.tarim.travel.utils.Constants;
import org.xinjiang.tarim.travel.utils.JsonResult;
import org.xinjiang.tarim.travel.vo.Criteria;
import org.xinjiang.tarim.travel.vo.PageBean;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/api/poi")
public class PoiApi {
    @Autowired
    PoiService poiService;

    /**
     * 获取景点
     * @param poi
     * @return
     */
    @RequestMapping(value = "/getPois")
    public JsonResult getPois(@RequestBody Poi poi){
        JsonResult jsonResult;
        try {
            List list = poiService.selectAllByMddId(poi);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "成功", list);
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "异常");
        }
        return jsonResult;
    }

    /**
     * 随机获取10个
     * @return
     */
    @RequestMapping(value = "/getTen")
    public JsonResult selectTenPoi(){
        JsonResult jsonResult;
        try {
            List list = poiService.selectTenPoi();
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "成功", list);
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "异常");
        }
        return jsonResult;
    }

    /**
     * 获取详情
     * @param poiId
     * @return
     */
    @RequestMapping(value = "/getDetail")
    public JsonResult getDetail(String poiId){
        JsonResult jsonResult;
        try {
            Map map = poiService.selectByPrimaryKey(poiId);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "成功", map);
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "异常");
        }
        return jsonResult;
    }

    /**
     * 获取关注景点
     * @param request
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/getLike")
    public JsonResult getLike(HttpServletRequest request,
                              @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        JsonResult jsonResult;
        try {
            String openid = request.getHeader("tokenId");
            if (openid == null || openid.equals("")) {
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "请先登录账号哟");
                return jsonResult;
            }
            PageBean pageBean = new PageBean(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("openid", openid);
            pageBean = poiService.selectConcerns(criteria, pageBean);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "获取成功", pageBean);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "获取异常");
        }
        return jsonResult;
    }
}
